<template>
	<div>
		<el-form :inline="true">
			<el-form-item>
				<el-select v-model="searchForm.companyType" placeholder="企业类型" clearable>
					<el-option label="造船企业" value="1"></el-option>
					<el-option label="配套企业" value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-input v-model="searchForm.districtCode" placeholder="行政代码" clearable> </el-input>
			</el-form-item>
			<el-form-item>
				<el-input v-model="searchForm.name" placeholder="公司名称" clearable> </el-input>
			</el-form-item>
			<el-form-item label="报告年份" prop="reportDate" label-width="100px">
                <el-date-picker
                    v-model="searchForm.reportDate"
                    type="year"
        			value-format="yyyy"
                    placeholder="选择年份">
                </el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-select v-model="searchForm.verifyStatus" placeholder="审核状态" clearable>
					<el-option label="未更新" value="WAIT"></el-option>
					<el-option label="已更新" value="PASS"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button @click="getList">搜索</el-button>
			</el-form-item>

			<el-form-item>
				<el-popconfirm title="这是确定批量删除吗？" @confirm="delHandle(null)">
					<el-button type="danger" slot="reference" :disabled="delBtlStatu" v-if="hasAuth('boat:yellow:company:delete')"
						>批量删除</el-button
					>
				</el-popconfirm>
			</el-form-item>
		</el-form>

		<el-table
			ref="multipleTable"
			:data="tableData"
			tooltip-effect="dark"
			style="width: 100%"
			border
			stripe
			@selection-change="handleSelectionChange"
		>
			<el-table-column align="center" fixed="left" type="selection" width="55"> </el-table-column>
			<el-table-column prop="name" align="center" label="企业名称"></el-table-column>
			<el-table-column prop="company_type_name" align="center" label="企业类型"></el-table-column>
			<el-table-column prop="description" align="center" label="企业描述"></el-table-column>
			<el-table-column prop="reportDate" align="center" width="100" label="报告时间"></el-table-column>
			<el-table-column align="center" width="100" prop="district_code" label="行政代码"> </el-table-column>
			<el-table-column align="center" width="100" prop="city_name" label="城市"> </el-table-column>
			<el-table-column prop="verify_status" align="center" width="100" label="状态">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.verify_status === 'PASS'" type="success"><i class="el-icon-success"></i> 已更新</el-tag>
					<el-tag v-else type="info">未更新</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="icon" align="center" width="180px" label="操作">
				<template slot-scope="scope">
					<el-button
						type="primary"
						plain
						@click="addUpdateButton(scope.row.id)"
						v-if="hasAuth('boat:yellow:company:edit')"
						>编辑</el-button
					>

					<template v-if="hasAuth('boat:yellow:company:delete')">
						<el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)">
							<el-button type="danger" plain slot="reference">删除</el-button>
						</el-popconfirm>
					</template>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			@size-change="handleSizeChange"
			@current-change="handleCurrentChange"
			layout="total, sizes, prev, pager, next, jumper"
			:page-sizes="[10, 20, 50, 100]"
			:current-page="current"
			:page-size="size"
			:total="total"
		>
		</el-pagination>
		<AddUpdate ref="addUpdate" v-if="addUpdateVisible" @refreshDataList="getList"></AddUpdate>
	</div>
</template>

<script>
import {
  codeToText,
} from "element-china-area-data";
import {formatServerDate} from '@/utils/date';
import AddUpdate from './AddUpdate.vue';
import TypeIdSelect from '@/components/typeIdSelect/Index.vue';
import NameIdSelect from '@/components/nameIdSelect/Index.vue';
export default {
	name: 'CompanyList',
	components: {
		AddUpdate,
		TypeIdSelect,
		NameIdSelect,
	},
	props: {
		type: {
			type: String,
			default: '1',
		}
	},
	data() {
		return {
			searchForm: {
				type: this.type,
            },
			delBtlStatu: true,
			tableData: [],
			total: 0,
			size: 10,
			current: 1,
			addUpdateVisible: false,
		};
	},
	created() {
		this.getList();
	},
	methods: {
		toggleSelection(rows) {
			if (rows) {
				rows.forEach((row) => {
					this.$refs.multipleTable.toggleRowSelection(row);
				});
			} else {
				this.$refs.multipleTable.clearSelection();
			}
		},
		handleSelectionChange(val) {
			console.log('勾选');
			console.log(val);
			this.multipleSelection = val;

			this.delBtlStatu = val.length == 0;
		},

		handleSizeChange(val) {
			console.log(`每页 ${val} 条`);
			this.size = val;
			this.getList();
		},
		handleCurrentChange(val) {
			console.log(`当前页: ${val}`);
			this.current = val;
			this.getList();
		},
		getList() {
            const formData = this.searchForm;
			this.$axios
				.get('/admin/boat/yellow/company/list', {
					params: {
						current: this.current,
						size: this.size,
                        ...formData,
					},
				})
				.then((res) => {
                    const result = res.result;
					const typeNameMap = {'1': '造船企业', '2': '配套企业'};
					this.tableData = result.records.map((item) => {
                        const {create_time,company_type, update_time,report_date, district_code, ...rest} = item;

						let city_name = '';
						if(district_code){
							city_name = codeToText[parseInt(district_code.substring(0, 4), 10)] || '';
						}
						if(city_name === '市辖区'){
							city_name = codeToText[parseInt(district_code, 10)];
						}
                        return {
                            create_time: formatServerDate(create_time),
                            update_time: formatServerDate(update_time),
							district_code,
							city_name,
							reportDate: report_date,
							company_type_name: typeNameMap[company_type],
                            ...rest,
                        }
                    });
					this.size = result.size;
					this.current = result.current;
					this.total = result.total;
				}).catch((e) => {
                    console.log(e);
                });
		},
		delHandle(id) {
			var ids = [];

			if (id) {
				ids.push(id);
			} else {
				this.multipleSelection.forEach((row) => {
					ids.push(row.id);
				});
			}

			this.$axios.get('/admin/boat/yellow/company/delByIds', {params:{ids: ids.join(',')}}).then((res) => {
				this.$message({
					showClose: true,
					message: '恭喜你，操作成功',
					type: 'success',
					onClose: () => {
						this.getList();
					},
				});
			});
		},
		addUpdateButton(id) {
			this.addUpdateVisible = true;
			this.$nextTick(() => {
				this.$refs.addUpdate.init(id);
			});
		},
	},
};
</script>

<style scoped>
.el-button {
	padding: 0 10px;
	height: 30px;
	line-height: 30px;
	font-size: 12px;
	margin-right: 10px;
}
</style>
